//
//  XMSurgeryTypeTableViewCell.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMSurgeryTypeTableViewCell.h"
#import "KBaseCollectionView.h"
#import "XMCameraTypeLayout.h"

#define numberCountInRow 3


@interface XMSurgeryTypeTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) KBaseCollectionView * collectionView;


@property (nonatomic,strong) UIPageControl * pageControl;
@end

@implementation XMSurgeryTypeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.contentView);
        make.height.equalTo(@20);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.contentView);
        make.bottom.equalTo(self.pageControl.mas_top);
    }];
}


#pragma mark ----collection----

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMSurgeryTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMSurgeryTypeCollectionViewCell className] forIndexPath:indexPath];
    
    if (indexPath.row >= self.listData.count) {
        cell.hidden = YES;
        cell.model = nil;
    }else{
        
        cell.hidden = NO;
        XMTypeListClassifyModel * classifyModel = self.listData[indexPath.row];
        cell.model = classifyModel;
        cell.itemSelected = classifyModel.isSelected;
    }
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.listData.count/(numberCountInRow*3)) {
        if (self.listData.count%(numberCountInRow*3)) {
            return (self.listData.count/(numberCountInRow*3)+1)*(numberCountInRow*3);
        }else{
            return self.listData.count;
        }
    }else{
        return self.listData.count;
    }
    

    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat  LRMargin = 13;
    
    NSInteger itemW = (kScreenWidth-LRMargin*numberCountInRow*2)/numberCountInRow;
    
    return CGSizeMake(itemW, KRealValue(40));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(8,13,8,13);//分别为上、左、下、右
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat scrollViewW = scrollView.frame.size.width;
    
    CGFloat x = scrollView.contentOffset.x;
    
    int page = (x + scrollViewW/2)/scrollViewW;
    
    _pageControl.currentPage = page;
    
}


#pragma mark ----getting&setting----
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];
        _pageControl.currentPageIndicatorTintColor = kMainBlueColor;
        _pageControl.pageIndicatorTintColor = HEX_RGB(0x858585);
        [self.contentView addSubview:_pageControl];
    }
    
    return _pageControl;
}
//列表
-(KBaseCollectionView *)collectionView{
    
    if (!_collectionView) {
        
        if (self.listData.count>= numberCountInRow *3) {
            XMCameraTypeLayout *layout = [[XMCameraTypeLayout alloc] init];
            [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
            layout.itemCountPerRow = numberCountInRow;
            layout.rowCount = 3;
            layout.minimumLineSpacing = 25;
            layout.minimumInteritemSpacing = 15;
            _collectionView = [[KBaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        }else{
            
             UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            _collectionView = [[KBaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        }
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kWhiteColor;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:_collectionView];
        
        [_collectionView registerClass:[XMSurgeryTypeCollectionViewCell class] forCellWithReuseIdentifier:[XMSurgeryTypeCollectionViewCell className]];
        
        
    }
    return _collectionView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XMTypeListClassifyModel * classifyModel = self.listData[indexPath.row];
    

    classifyModel.isSelected = !classifyModel.isSelected;
    
    if (self.selectItemBlock) {
        self.selectItemBlock(indexPath.row,classifyModel.isSelected);
    }
    [self.collectionView reloadData];

    
}

-(void)setListData:(NSArray *)listData{
    _listData = listData;
    [self.collectionView removeFromSuperview];
    self.collectionView = nil;
    
    if (listData.count/(numberCountInRow*3)) {
        [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.equalTo(self.contentView);
            make.height.equalTo(@20);
        }];
        
        
        if (listData.count%(numberCountInRow*3)) {
            _pageControl.numberOfPages = listData.count/(numberCountInRow*3)+1;
        }else{
            _pageControl.numberOfPages = listData.count/(numberCountInRow*3);
            
        }
        
        self.pageControl.hidden = NO;

    }else{
        [self.pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.equalTo(self.contentView);
            make.height.equalTo(@0);
        }];
        self.pageControl.hidden = YES;
    }

    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.contentView);
        make.bottom.equalTo(self.pageControl.mas_top);
    }];

    [self.collectionView reloadData];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface XMSurgeryTypeCollectionViewCell ()
//
@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) UIImageView * picImageView;

@end

@implementation XMSurgeryTypeCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    self.layer.cornerRadius = 4;
    self.clipsToBounds = YES;
    self.backgroundColor = KItemNormalColor;
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.width.equalTo(@28);
        make.height.equalTo(@21);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.left.equalTo(self).offset(3);
        make.right.equalTo(self).offset(-3);

    }];
    
    [self bringSubviewToFront:self.picImageView];
    
}

#pragma mark ----getting&setting----
//图片
-(UIImageView *)picImageView{
    if (!_picImageView) {
        _picImageView = [[UIImageView alloc]init];
        
        [self addSubview:_picImageView];
    }
    return _picImageView;
}


//角度文字
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = kTextBlackColor;
        _titleLabel.font = kFont(14);
        _titleLabel.text = @"隆鼻";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = 2;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}


-(void)setItemSelected:(BOOL)itemSelected{
    _itemSelected = itemSelected;
    
    if (itemSelected) {
        [_picImageView setImage:kImage(@"surgeryType_selected")];
        self.backgroundColor = KItemSelectedColor;

    }else{
        [_picImageView setImage:nil];
        self.backgroundColor = KItemNormalColor;
    }
}

-(void)setModel:(XMTypeListClassifyModel *)model{
    _model = model;
    self.titleLabel.text = model.name;
}

@end
